home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / cmd.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  7.8 KB  |  340 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import string
  5. __all__ = [
  6.     'Cmd']
  7. PROMPT = '(Cmd) '
  8. IDENTCHARS = string.ascii_letters + string.digits + '_'
  9.  
  10. class Cmd:
  11.     prompt = PROMPT
  12.     identchars = IDENTCHARS
  13.     ruler = '='
  14.     lastcmd = ''
  15.     intro = None
  16.     doc_leader = ''
  17.     doc_header = 'Documented commands (type help <topic>):'
  18.     misc_header = 'Miscellaneous help topics:'
  19.     undoc_header = 'Undocumented commands:'
  20.     nohelp = '*** No help on %s'
  21.     use_rawinput = 1
  22.     
  23.     def __init__(self, completekey = 'tab', stdin = None, stdout = None):
  24.         import sys
  25.         if stdin is not None:
  26.             self.stdin = stdin
  27.         else:
  28.             self.stdin = sys.stdin
  29.         if stdout is not None:
  30.             self.stdout = stdout
  31.         else:
  32.             self.stdout = sys.stdout
  33.         self.cmdqueue = []
  34.         self.completekey = completekey
  35.  
  36.     
  37.     def cmdloop(self, intro = None):
  38.         self.preloop()
  39.         if self.use_rawinput and self.completekey:
  40.             
  41.             try:
  42.                 import readline
  43.                 self.old_completer = readline.get_completer()
  44.                 readline.set_completer(self.complete)
  45.                 readline.parse_and_bind(self.completekey + ': complete')
  46.             except ImportError:
  47.                 pass
  48.             except:
  49.                 None<EXCEPTION MATCH>ImportError
  50.             
  51.  
  52.         None<EXCEPTION MATCH>ImportError
  53.         
  54.         try:
  55.             if intro is not None:
  56.                 self.intro = intro
  57.             
  58.             if self.intro:
  59.                 self.stdout.write(str(self.intro) + '\n')
  60.             
  61.             stop = None
  62.             while not stop:
  63.                 if self.cmdqueue:
  64.                     line = self.cmdqueue.pop(0)
  65.                 elif self.use_rawinput:
  66.                     
  67.                     try:
  68.                         line = raw_input(self.prompt)
  69.                     except EOFError:
  70.                         line = 'EOF'
  71.                     except:
  72.                         None<EXCEPTION MATCH>EOFError
  73.                     
  74.  
  75.                 None<EXCEPTION MATCH>EOFError
  76.                 self.stdout.write(self.prompt)
  77.                 self.stdout.flush()
  78.                 line = self.stdin.readline()
  79.                 if not len(line):
  80.                     line = 'EOF'
  81.                 else:
  82.                     line = line[:-1]
  83.                 line = self.precmd(line)
  84.                 stop = self.onecmd(line)
  85.                 stop = self.postcmd(stop, line)
  86.             self.postloop()
  87.         finally:
  88.             if self.use_rawinput and self.completekey:
  89.                 
  90.                 try:
  91.                     import readline
  92.                     readline.set_completer(self.old_completer)
  93.                 except ImportError:
  94.                     pass
  95.                 except:
  96.                     None<EXCEPTION MATCH>ImportError
  97.                 
  98.  
  99.  
  100.  
  101.     
  102.     def precmd(self, line):
  103.         return line
  104.  
  105.     
  106.     def postcmd(self, stop, line):
  107.         return stop
  108.  
  109.     
  110.     def preloop(self):
  111.         pass
  112.  
  113.     
  114.     def postloop(self):
  115.         pass
  116.  
  117.     
  118.     def parseline(self, line):
  119.         line = line.strip()
  120.         if not line:
  121.             return (None, None, line)
  122.         elif line[0] == '?':
  123.             line = 'help ' + line[1:]
  124.         elif line[0] == '!':
  125.             if hasattr(self, 'do_shell'):
  126.                 line = 'shell ' + line[1:]
  127.             else:
  128.                 return (None, None, line)
  129.         
  130.         i = 0
  131.         n = len(line)
  132.         while i < n and line[i] in self.identchars:
  133.             i = i + 1
  134.         cmd = line[:i]
  135.         arg = line[i:].strip()
  136.         return (cmd, arg, line)
  137.  
  138.     
  139.     def onecmd(self, line):
  140.         (cmd, arg, line) = self.parseline(line)
  141.         if not line:
  142.             return self.emptyline()
  143.         
  144.         if cmd is None:
  145.             return self.default(line)
  146.         
  147.         self.lastcmd = line
  148.         if cmd == '':
  149.             return self.default(line)
  150.         else:
  151.             
  152.             try:
  153.                 func = getattr(self, 'do_' + cmd)
  154.             except AttributeError:
  155.                 return self.default(line)
  156.  
  157.             return func(arg)
  158.  
  159.     
  160.     def emptyline(self):
  161.         if self.lastcmd:
  162.             return self.onecmd(self.lastcmd)
  163.         
  164.  
  165.     
  166.     def default(self, line):
  167.         self.stdout.write('*** Unknown syntax: %s\n' % line)
  168.  
  169.     
  170.     def completedefault(self, *ignored):
  171.         return []
  172.  
  173.     
  174.     def completenames(self, text, *ignored):
  175.         dotext = 'do_' + text
  176.         return _[1]
  177.  
  178.     
  179.     def complete(self, text, state):
  180.         if state == 0:
  181.             import readline
  182.             origline = readline.get_line_buffer()
  183.             line = origline.lstrip()
  184.             stripped = len(origline) - len(line)
  185.             begidx = readline.get_begidx() - stripped
  186.             endidx = readline.get_endidx() - stripped
  187.             if begidx > 0:
  188.                 (cmd, args, foo) = self.parseline(line)
  189.             None if cmd == '' else None<EXCEPTION MATCH>AttributeError
  190.             compfunc = self.completenames
  191.             self.completion_matches = compfunc(text, line, begidx, endidx)
  192.         
  193.         
  194.         try:
  195.             return self.completion_matches[state]
  196.         except IndexError:
  197.             return None
  198.  
  199.  
  200.     
  201.     def get_names(self):
  202.         names = []
  203.         classes = [
  204.             self.__class__]
  205.         while classes:
  206.             aclass = classes.pop(0)
  207.             if aclass.__bases__:
  208.                 classes = classes + list(aclass.__bases__)
  209.             
  210.             names = names + dir(aclass)
  211.         return names
  212.  
  213.     
  214.     def complete_help(self, *args):
  215.         return self.completenames(*args)
  216.  
  217.     
  218.     def do_help(self, arg):
  219.         if arg:
  220.             
  221.             try:
  222.                 func = getattr(self, 'help_' + arg)
  223.             except AttributeError:
  224.                 
  225.                 try:
  226.                     doc = getattr(self, 'do_' + arg).__doc__
  227.                     if doc:
  228.                         self.stdout.write('%s\n' % str(doc))
  229.                         return None
  230.                 except AttributeError:
  231.                     pass
  232.  
  233.                 self.stdout.write('%s\n' % str(self.nohelp % (arg,)))
  234.                 return None
  235.  
  236.             func()
  237.         else:
  238.             names = self.get_names()
  239.             cmds_doc = []
  240.             cmds_undoc = []
  241.             help = { }
  242.             for name in names:
  243.                 if name[:5] == 'help_':
  244.                     help[name[5:]] = 1
  245.                     continue
  246.             
  247.             names.sort()
  248.             prevname = ''
  249.             for name in names:
  250.                 if name[:3] == 'do_':
  251.                     if name == prevname:
  252.                         continue
  253.                     
  254.                     prevname = name
  255.                     cmd = name[3:]
  256.                     if cmd in help:
  257.                         cmds_doc.append(cmd)
  258.                         del help[cmd]
  259.                     elif getattr(self, name).__doc__:
  260.                         cmds_doc.append(cmd)
  261.                     else:
  262.                         cmds_undoc.append(cmd)
  263.                 cmd in help
  264.             
  265.             self.stdout.write('%s\n' % str(self.doc_leader))
  266.             self.print_topics(self.doc_header, cmds_doc, 15, 80)
  267.             self.print_topics(self.misc_header, help.keys(), 15, 80)
  268.             self.print_topics(self.undoc_header, cmds_undoc, 15, 80)
  269.  
  270.     
  271.     def print_topics(self, header, cmds, cmdlen, maxcol):
  272.         if cmds:
  273.             self.stdout.write('%s\n' % str(header))
  274.             if self.ruler:
  275.                 self.stdout.write('%s\n' % str(self.ruler * len(header)))
  276.             
  277.             self.columnize(cmds, maxcol - 1)
  278.             self.stdout.write('\n')
  279.         
  280.  
  281.     
  282.     def columnize(self, list, displaywidth = 80):
  283.         if not list:
  284.             self.stdout.write('<empty>\n')
  285.             return None
  286.         
  287.         nonstrings = _[1]
  288.         size = len(list)
  289.         if size == 1:
  290.             self.stdout.write('%s\n' % str(list[0]))
  291.             return None
  292.         
  293.         for nrows in range(1, len(list)):
  294.             ncols = (size + nrows - 1) // nrows
  295.             colwidths = []
  296.             totwidth = -2
  297.             for col in range(ncols):
  298.                 colwidth = 0
  299.                 for row in range(nrows):
  300.                     i = row + nrows * col
  301.                     if i >= size:
  302.                         break
  303.                     
  304.                     x = list[i]
  305.                     colwidth = max(colwidth, len(x))
  306.                 
  307.                 colwidths.append(colwidth)
  308.                 totwidth += colwidth + 2
  309.                 if totwidth > displaywidth:
  310.                     break
  311.                     continue
  312.             
  313.             if totwidth <= displaywidth:
  314.                 break
  315.                 continue
  316.         else:
  317.             nrows = len(list)
  318.             ncols = 1
  319.             colwidths = [
  320.                 0]
  321.         for row in range(nrows):
  322.             texts = []
  323.             for col in range(ncols):
  324.                 i = row + nrows * col
  325.                 if i >= size:
  326.                     x = ''
  327.                 else:
  328.                     x = list[i]
  329.                 texts.append(x)
  330.             
  331.             while texts and not texts[-1]:
  332.                 del texts[-1]
  333.             for col in range(len(texts)):
  334.                 texts[col] = texts[col].ljust(colwidths[col])
  335.             
  336.             self.stdout.write('%s\n' % str('  '.join(texts)))
  337.         
  338.  
  339.  
  340.